package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingLevelServiceImpl implements NursingLevelService {
    final NursingLevelMapper nursingLevelMapper;

    /**
     * 分页查询护理等级信息
     *
     * @param dto 护理等级分页条件查询Dto
     * @return {@code PageBean<NursingLevelVo> }
     * @author YiSheep
     */
    @Override
    public PageBean<NursingLevelVo> pageQuery(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingLevelVo> page = nursingLevelMapper.pageQuery(dto);
        PageBean<NursingLevelVo> pageBean = PageBean.of(page, NursingLevelVo.class);
        return pageBean;
    }

    /**
     * 插入护理等级信息
     *
     * @param dto 护理等级Dto
     * @author YiSheep
     */
    @Override
    public void addNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.addNursingLevel(nursingLevel);
    }

    /**
     * 根据ID查询护理等级信息
     *
     * @param id 护理等级ID
     * @return {@code NursingLevelVo }
     * @author YiSheep
     */
    @Override
    public NursingLevelVo getDetailById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getDetailById(id);
        NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
        return nursingLevelVo;
    }

    /**
     * 更新护理等级信息
     *
     * @param dto 护理等级Dto
     * @author YiSheep
     */
    @Override
    public void updateNursingLevel(NursingLevelDto dto) {
        if (dto.getStatus() == 1) {
            throw new BaseException(BasicEnum.UPDATE_LEVEL_STATUS_FAIL);
        }
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级ID
     * @author YiSheep
     */
    @Override
    public void deleteById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getDetailById(id);
        if (nursingLevel.getStatus() == 1) {
            throw new BaseException(BasicEnum.DELETE_LEVEL_STATUS_FAIL);
        }
        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用/禁用护理等级
     *
     * @param id     护理等级ID
     * @param status 状态
     * @author YiSheep
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingLevel nursingLevel = NursingLevel.builder().id(id).status(status).build();
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 查询所有护理等级信息
     *
     * @return {@code List<NursingLevel> }
     * @author YiSheep
     */
    @Override
    public List<NursingLevel> getAllNursingLevel() {
        return nursingLevelMapper.getAllNursingLevel();
    }
}


